#!/bin/sh /etc/rc.common
# Copyright (C) 2023 Tianling Shen <cnsztl@immortalwrt.org>

USE_PROCD=1
START=99

CONF="dae"
PROG="/usr/bin/dae"

start_service() {
	config_load "$CONF"

	local enabled
	config_get_bool enabled "config" "enabled" "0"
	[ "$enabled" -eq "1" ] || return 1

	local config_file
	config_get config_file "config" "config_file" "/etc/dae/config.dae"

	"$PROG" validate -c "$config_file" || return 1

	procd_open_instance "$CONF"
	procd_set_param command "$PROG"
	procd_append_param command run -c "$config_file"

	procd_set_param limits core="unlimited"
	procd_set_param limits nofile="1000000 1000000"
	procd_set_param respawn
	# procd_set_param stdout 1
	procd_set_param stderr 1

	procd_close_instance
}

service_triggers() {
	procd_add_reload_trigger "$CONF"
}

reload_service() {
	"$PROG" reload "$(cat /var/run/dae.pid)"
}
